package com.yc.thread;

import java.util.ArrayList;
import java.util.List;

public class TestVolatile {
	
	volatile List<String> content = new ArrayList<>();
	
	/**
	 * 两个线程：一个往list里新增数据，另外一个监控，当发现数量到5时输出
	 */
	public void m1() {
		Thread t1 = new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				content.add(i + "");
				System.out.println(i);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "t1");
		
		
		Thread t2 = new Thread(() -> {
			for (; ;) {
				if (content.size() == 5) {
					System.out.println("-----get it-------");
					break;
				}
//				try {
//					Thread.sleep(10);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
			}
		}, "t2");
		
		t1.start();
		t2.start();
		
	}

	public static void main(String[] args) {
		TestVolatile tv = new TestVolatile();
		tv.m1();
	}
}
